﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormAddCustomer : FormTemplet
    {
        public FormAddCustomer()
        {
            this.InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Coustomer_Create])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                this.txtCompanyName.Text = CommonMethod.Fun1(this.txtCompanyName.Text);
                this.txtLeader.Text = CommonMethod.Fun1(this.txtLeader.Text);
                this.txtLeaderPhone.Text = CommonMethod.Fun1(this.txtLeaderPhone.Text);
                this.txtCompanyPhone.Text = CommonMethod.Fun1(this.txtCompanyPhone.Text);
                this.txtFax.Text = CommonMethod.Fun1(this.txtFax.Text);
                this.txtCompanyAddress.Text = CommonMethod.Fun1(this.txtCompanyAddress.Text);
                this.txtLinkman.Text = CommonMethod.Fun1(this.txtLinkman.Text);
                this.txtLinkmanPhone.Text = CommonMethod.Fun1(this.txtLinkmanPhone.Text);
                this.txtEmail.Text = CommonMethod.Fun1(this.txtEmail.Text);
                this.cbType.Text = CommonMethod.Fun1(this.cbType.Text);
                if (this.txtCompanyName.Text.Trim().Length > 32 || this.txtCompanyName.Text.Trim() == "")
                {
                    MessageBox.Show("公司名称超长了或者长度为0，应该在32个字符之内", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.txtCompanyName.Focus();
                }
                else
                {
                    if (this.txtLeader.Text.Trim() != "")
                    {
                        if (this.txtLeader.Text.Trim().Length > 16 || this.txtLeader.Text.Trim().Length == 0)
                        {
                            MessageBox.Show("负责人姓名 应在1-16个字符之间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.txtLeader.Focus();
                            return;
                        }
                    }
                    if (this.txtLeaderPhone.Text.Trim() != "")
                    {
                        if (this.txtLeaderPhone.Text.Trim().Length > 32 || this.txtLeaderPhone.Text.Trim().Length < 6)
                        {
                            MessageBox.Show("电话号码 超长 或者 太短，应该在 6-32 个字符之间", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.txtLeaderPhone.Focus();
                            return;
                        }
                    }
                    if (this.txtCompanyPhone.Text.Trim() != "")
                    {
                        if (this.txtCompanyPhone.Text.Trim().Length > 32 || this.txtCompanyPhone.Text.Trim().Length < 7)
                        {
                            MessageBox.Show("电话号码 超长 或者 太短，应该在7-32个字符之间", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.txtCompanyPhone.Focus();
                            return;
                        }
                    }
                    if (this.txtFax.Text.Trim() != "")
                    {
                        if (this.txtFax.Text.Trim().Length > 32 || this.txtFax.Text.Trim().Length < 7)
                        {
                            MessageBox.Show("传真号码太短，应该在7-32个字符之间 或者 不填", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.txtFax.Focus();
                            return;
                        }
                    }
                    if (this.txtCompanyAddress.Text.Trim().Length > 64 || this.txtCompanyAddress.Text.Trim() == "")
                    {
                        MessageBox.Show("地址名称超长了或者长度为0，应该在64个字符之内", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtCompanyAddress.Focus();
                    }
                    else if (this.txtLinkman.Text.Trim().Length > 16 || this.txtLinkman.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("姓名超长了或者长度为0，应该在16个字符之内", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtLinkman.Focus();
                    }
                    else
                    {
                        if (this.txtLinkmanPhone.Text.Trim() != "")
                        {
                            if (this.txtLinkmanPhone.Text.Trim().Length > 32 || this.txtLinkmanPhone.Text.Trim().Length < 7)
                            {
                                MessageBox.Show("电话号码 超长 或者 太短，应该在7-32个字符之间", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                this.txtLinkmanPhone.Focus();
                                return;
                            }
                        }
                        if (this.txtEmail.Text.Trim().Length > 32)
                        {
                            MessageBox.Show("邮件名称超长了，应该在32个字符之内；或者是不填写", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.txtEmail.Focus();
                        }
                        else if (this.cbType.Text.Trim().Length > 16 || this.cbType.Text.Trim() == "")
                        {
                            MessageBox.Show("客户类型不能为空，且字符在16个字符之内!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.cbType.Focus();
                        }
                        else
                        {
                            Customers customers = new Customers();
                            bool flag = false;
                            try
                            {
                                flag = customers.InsertCustomer(this.txtCompanyName.Text.Trim(), this.txtLeader.Text.Trim(), this.txtLeaderPhone.Text.Trim(), this.txtCompanyPhone.Text.Trim(), this.txtFax.Text.Trim(), this.txtCompanyAddress.Text.Trim(), this.txtLinkman.Text.Trim(), this.txtLinkmanPhone.Text.Trim(), this.txtEmail.Text.Trim(), this.cbType.Text.Trim(), this.ckAddProject.Checked);
                            }
                            catch (SqlException ex)
                            {
                                int number = ex.Number;
                                if (number != 2627)
                                {
                                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                                else
                                {
                                    MessageBox.Show("不能存在两个 公司名称 和 联系人 相同的客户！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(ex2.Message, "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            customers.Dispose();
                            if (flag)
                            {
                                MessageBox.Show(this.ckAddProject.Checked ? "客户、计划全部录入成功!" : "客户录入成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                base.Close();
                            }
                        }
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
